package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.RoadWorkBo;
import org.dromara.system.domain.vo.RoadWorkVo;
import org.dromara.system.domain.RoadWork;
import org.dromara.system.mapper.RoadWorkMapper;
import org.dromara.system.service.IRoadWorkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 断路作业Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RoadWorkServiceImpl implements IRoadWorkService {

    private final RoadWorkMapper baseMapper;

    /**
     * 查询断路作业
     *
     * @param id 主键
     * @return 断路作业
     */
    @Override
    public RoadWorkVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询断路作业列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 断路作业分页列表
     */
    @Override
    public TableDataInfo<RoadWorkVo> queryPageList(RoadWorkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RoadWork> lqw = buildQueryWrapper(bo);
        Page<RoadWorkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的断路作业列表
     *
     * @param bo 查询条件
     * @return 断路作业列表
     */
    @Override
    public List<RoadWorkVo> queryList(RoadWorkBo bo) {
        LambdaQueryWrapper<RoadWork> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RoadWork> buildQueryWrapper(RoadWorkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RoadWork> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RoadWork::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getZyzbh()), RoadWork::getZyzbh, bo.getZyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getSqdw()), RoadWork::getSqdw, bo.getSqdw());
        lqw.eq(bo.getZysqsj() != null, RoadWork::getZysqsj, bo.getZysqsj());
        lqw.eq(StringUtils.isNotBlank(bo.getSqr()), RoadWork::getSqr, bo.getSqr());
        lqw.eq(StringUtils.isNotBlank(bo.getDlyy()), RoadWork::getDlyy, bo.getDlyy());
        lqw.eq(StringUtils.isNotBlank(bo.getZynr()), RoadWork::getZynr, bo.getZynr());
        lqw.eq(StringUtils.isNotBlank(bo.getZywz()), RoadWork::getZywz, bo.getZywz());
        lqw.eq(StringUtils.isNotBlank(bo.getXgdwbm()), RoadWork::getXgdwbm, bo.getXgdwbm());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwlx()), RoadWork::getZydwlx, bo.getZydwlx());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwmc()), RoadWork::getZydwmc, bo.getZydwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getZyfzr()), RoadWork::getZyfzr, bo.getZyfzr());
        lqw.eq(bo.getZysskssj() != null, RoadWork::getZysskssj, bo.getZysskssj());
        lqw.eq(bo.getZyssjssj() != null, RoadWork::getZyssjssj, bo.getZyssjssj());
        lqw.eq(StringUtils.isNotBlank(bo.getDlddsyt()), RoadWork::getDlddsyt, bo.getDlddsyt());
        lqw.eq(StringUtils.isNotBlank(bo.getZyfwFs()), RoadWork::getZyfwFs, bo.getZyfwFs());
        lqw.eq(StringUtils.isNotBlank(bo.getBzrmc()), RoadWork::getBzrmc, bo.getBzrmc());
        lqw.eq(bo.getBzsj() != null, RoadWork::getBzsj, bo.getBzsj());
        lqw.eq(StringUtils.isNotBlank(bo.getSfsjqttszy()), RoadWork::getSfsjqttszy, bo.getSfsjqttszy());
        lqw.eq(StringUtils.isNotBlank(bo.getSjqttszyzbh()), RoadWork::getSjqttszyzbh, bo.getSjqttszyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFxbs()), RoadWork::getFxbs, bo.getFxbs());
        lqw.eq(StringUtils.isNotBlank(bo.getAqjdr()), RoadWork::getAqjdr, bo.getAqjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJsjdr()), RoadWork::getJsjdr, bo.getJsjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJhr()), RoadWork::getJhr, bo.getJhr());
        lqw.eq(StringUtils.isNotBlank(bo.getDqzt()), RoadWork::getDqzt, bo.getDqzt());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), RoadWork::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), RoadWork::getLatitude, bo.getLatitude());
        lqw.eq(bo.getStartTime() != null, RoadWork::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, RoadWork::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSpgbbm()), RoadWork::getSpgbbm, bo.getSpgbbm());
        lqw.eq(StringUtils.isNotBlank(bo.getFhyp()), RoadWork::getFhyp, bo.getFhyp());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachment()), RoadWork::getAttachment, bo.getAttachment());
        return lqw;
    }

    /**
     * 新增断路作业
     *
     * @param bo 断路作业
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RoadWorkBo bo) {
        RoadWork add = MapstructUtils.convert(bo, RoadWork.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改断路作业
     *
     * @param bo 断路作业
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RoadWorkBo bo) {
        RoadWork update = MapstructUtils.convert(bo, RoadWork.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RoadWork entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除断路作业信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
